package com.snake.vargoappstore.loader;

import android.content.Context;

import com.snake.mylibrary.loader.DynamicLoader;
import com.snake.mylibrary.loader.DynamicResult;
import com.snake.mylibrary.net.BaseResult;
import com.snake.mylibrary.net.HttpStatus;
import com.snake.mylibrary.net.OkHttpManager;
import com.snake.mylibrary.runtime.DeviceInfo;
import com.snake.vargoappstore.AppConst;
import com.snake.vargoappstore.result.HomeFragmentResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public class HomeFragmentLoader extends DynamicLoader {

    public HomeFragmentLoader(Context context) {
        super(context);
    }

    @Override
    protected DynamicResult createDynamicResult() {
        BaseResult result = OkHttpManager.getInstance().httpGet(buildUrl());
        HttpStatus status = result.getStatus();
        if(status !=HttpStatus.HTTP_OK){
            return new HomeFragmentResult(null, status);
        }else{
            JSONObject obj = null;
            try {
                obj= new JSONObject(result.getJson());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return new HomeFragmentResult(obj,status);
        }

    }

    private String buildUrl() {
        HashMap<String,String> params = new HashMap<>();
        if(needBaseParams()){
            params.putAll(getBaseParams());
        }
        params.put("type","1");
        params.put("page","1");
        params.put("num","20");
        params.put("lastModifyTime",String.valueOf(0l));
        StringBuilder sb = new StringBuilder();
        sb.append("?");
        for(String key:params.keySet()){
            try {
                sb.append(URLEncoder.encode(key,"UTF-8"));
                sb.append("=");
                sb.append(URLEncoder.encode(params.get(key),"UTF-8"));
            } catch (Exception e) {
                sb.append("");
            }
            sb.append("&");
        }

        return AppConst.Api.HOME_URL+sb.deleteCharAt(sb.length()-1).toString();
    }

    private Map<String,String> getBaseParams() {
        HashMap<String, String> baseParams = new HashMap<>();
        baseParams.put("appId", AppConst.APP_ID);
        baseParams.put("channelId", AppConst.CHANNEL_ID);
        baseParams.put("product_id", AppConst.PRODUCT_ID);
        baseParams.put("cVer", ""+DeviceInfo.getVersion());
        baseParams.put("model", DeviceInfo.getModel());
        baseParams.put("brand", DeviceInfo.getBrand());
        baseParams.put("sysVer", "Android"+DeviceInfo.getSystemVersion());
        baseParams.put("width", String.valueOf(DeviceInfo.getScreenWidth()));
        baseParams.put("height", String.valueOf(DeviceInfo.getScreenHeight()));
        baseParams.put("imei", DeviceInfo.getImei());
        baseParams.put("channel", AppConst.channel_id);
        baseParams.put("subchannel", "0000");
        baseParams.put("signature", "");

        return baseParams;
    }

    private boolean needBaseParams(){
        return true;
    }

}
